<?php
namespace services;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class MessageService{
  const accessKeyId="LTAI4GERBqo8JSGaFukZFTFn";
  const accessKeySecret="9MmVfyXWwCeeHa1TxuokZnqznw5YHh";
  private $signName;
  public function __construct($signName){
    $this->signName = $signName;
  }



  public function send_message($phone,$template,$param = '')
  {
    AlibabaCloud::accessKeyClient(self::accessKeyId, self::accessKeySecret)
                        ->regionId('cn-hangzhou')
                        ->asDefaultClient();

    try {
        $result = AlibabaCloud::rpc()
                  ->product('Dysmsapi')
                  // ->scheme('https') // https | http
                  ->version('2017-05-25')
                  ->action('SendSms')
                  ->method('POST')
                  ->host('dysmsapi.aliyuncs.com')
                  ->options([
                      'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $phone,
                        'SignName' => $this->signName,
                        'TemplateCode' => $template,
                        'TemplateParam' => $param,
                      ],
                    ])
                  ->request();
        return $result->toArray();
    } catch (ClientException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    } catch (ServerException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    }

  }

}
